#include "goset.h"
#include "ui_goset.h"
#include "QDebug"
goset::goset(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::goset)
{

    ui->setupUi(this);
    m_persent = 1;//绘制的环的比例

       m_updateTimer = new QTimer(this);
       connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(UpdateAngle()));
       m_updateTimer->start(20);//启动定时器

       setWindowFlags(Qt::FramelessWindowHint);//无窗体
       setAttribute(Qt::WA_TranslucentBackground);//背景透明
       font_size=15;
       round_color=QColor(255, 107, 107);
       round_wigth=8;
       round_text="";
}

goset::~goset()
{
    delete ui;
}
void goset::set_val(int val)
{
    m_persent=val;
}
void goset::set_Text(QString val)
{
    round_text=val;
}
void goset::set_round_color(QColor Qcolor_val)
{
    round_color=Qcolor_val;
}

void goset::set_font_size(int val)
{
    font_size=val;

}
void goset::set_round_wigth(int val)//设定圈的宽度
{
    round_wigth=val;
}
void goset::set_gif(int val)//设定正转动角度
{
    gif_val=val;
}
void goset::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);

    int m_rotateAngle = 360*m_persent/100;
    int side = qMin(width(), height());
    QRectF outRect(0, 0, side, side);//矩形长宽为窗口的长宽
    QRectF inRect(round_wigth, round_wigth, side-round_wigth*2, side-round_wigth*2);
    QString valueStr = QString("%1%").arg(QString::number(m_persent));

    //画外圆
    p.setPen(Qt::NoPen);
    p.setBrush(QBrush(QColor(97, 117, 118)));
    p.drawEllipse(outRect);
    //画内圆

    if(gif_run)gif_start-=gif_val;//设定正转动角度

    p.setBrush(QBrush(round_color));
    p.drawPie(outRect, (gif_start-m_rotateAngle)*16, m_rotateAngle*16);//gif_start=90
    //画遮罩，遮罩颜色为窗口颜色
    p.setBrush(palette().window().color());
    p.drawEllipse(inRect);
    //画文字
    QFont f = QFont("Microsoft YaHei", font_size, QFont::Bold);
    p.setFont(f);
    p.setFont(f);
    p.setPen(QColor("#555555"));
    if(round_text=="")p.drawText(inRect, Qt::AlignCenter, valueStr);
    else p.drawText(inRect, Qt::AlignCenter, round_text);

}
void goset::UpdateAngle()
{
    repaint();
}
void goset::mousePressEvent(QMouseEvent *event)
{
    //如果单击了就触发clicked信号
    if (event->button() == Qt::LeftButton)
    {
        //触发clicked信号

        emit m_clicked(gif_run,this->objectName());
    }

}
